/* leveled-gui.cxx */


#include "leveled-gui.hxx"
#include "scrogui.hxx"
#include "../src/enemy.hxx"


SDL_Event event;

unsigned int main_menue(void)
{
	char about_string[50];
	unsigned int quit = 0;
	printf("init main menue...\n");
	SDL_Rect gui_pane;
	gui_pane.x = 20;
	gui_pane.y = 20;
	gui_pane.w = 270;
	gui_pane.h = YMAX - 40;
	scrogui * main_menue = new scrogui(&gui_pane, screen, "LevelEd: the scro level editor");
	main_menue->addButton("button1", "Generate a new level", 30, 100, &wizard_generate);
	main_menue->addButton("button2", "Open a level", 30, 120, &file_open_dialog);
	main_menue->addButton("button5", "Create a new enemy collection", 30, 150, &wizard_create_sei);
	main_menue->addButton("button6", "Open an enemy collection", 30, 170, &sei_open_dialog);
	main_menue->addButton("button7", "Tile editor", 30, 200, NULL);
	main_menue->addButton("button8", "Settings", 30, 230, &settings_dialog);
	main_menue->addButton("button4", "About", 30, 260, NULL);
	main_menue->addButton("button3", "Exit", 30, 300, NULL);
	sprintf(about_string, "levelEd, the scro level editor, %s.", __LEVELED_VERSION);
	main_menue->redrawGUI(NULL);
	SDL_Flip(screen);
	while (!quit)
	{
		if (SDL_PollEvent(&event))
		{
			main_menue->guiEventHandler(&event);
			main_menue->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());
		if (main_menue->get_flags_of_object("button3")) quit = 1;
		if (main_menue->get_flags_of_object("button4"))
		{
//		    fade_sdl_surface(screen, 120);
		    main_menue->message_box("About levelEd...", about_string, 0);
		    main_menue->set_flags_of_object("button4", main_menue->get_flags_of_object("button4") ^ SCROGUI_BUTTON_PRESSED);
		    main_menue->redrawGUI(NULL);
		    SDL_Flip(screen);
		}
	}
	delete(main_menue);
	return 1;
}

unsigned int settings_dialog(unsigned int parameter)
{
    unsigned int quit = 0;
    SDL_Rect gui_pane;
    gui_pane.x = 200;
    gui_pane.y = 200;
    gui_pane.w = 300;
    gui_pane.h = 150;
    scrogui * settings_dialog = new scrogui(&gui_pane, screen, "Settings");
    struct leveled_config_struct leveled_config = leveled_config_load_settings();
    settings_dialog->setBlank(0);
   	fade_sdl_surface(screen, SCROGUI_DEFAULT_FADE);

#ifndef WIN32
    	settings_dialog->addButton("button2", "toggle fullscreen/window mode", 40, 20, NULL);
#endif

    settings_dialog->addButton("button1", "Abort", 40, 100, NULL);
    settings_dialog->redrawGUI(NULL);
    SDL_Flip(screen);
    while(!quit)
    {
        if (SDL_PollEvent(&event))
        {
            settings_dialog->guiEventHandler(&event);
            settings_dialog->redrawGUI(NULL);
            SDL_Flip(screen);
        }
        SDL_Delay(TimeLeft());
		if (settings_dialog->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED)
		{
		    quit = 1;
		}
		
		if (settings_dialog->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED)
		{
			if (!SDL_WM_ToggleFullScreen(screen))
			{
				logger.log(1, "toggling window/fullscreen not possible!");
			} else
			{
				leveled_config.flags = screen->flags;
				if (!leveled_config_save_settings(leveled_config))
				{
					logger.log(2, "an error occured while saving the config.");
				}
				quit = 1;
			}
			settings_dialog->set_flags_of_object("button2", settings_dialog->get_flags_of_object("button2") ^ SCROGUI_BUTTON_PRESSED);
		}
    }
    delete(settings_dialog);
}

unsigned int file_open_dialog(unsigned int parameter)
{
	unsigned int quit = 0;
	unsigned int leveled_ret = 0;
	SDL_Rect gui_pane;
	char * filename_string = new char [255];
	struct leveled_config_struct leveled_config = leveled_config_load_settings();
	strcpy(filename_string, "level.sli");
	gui_pane.x = 200;
	gui_pane.y = 200;
	gui_pane.w = 300;
	gui_pane.h = 150;
	scrogui * file_open_dialog = new scrogui(&gui_pane, screen, "Enter file to open");
	file_open_dialog->setBlank(0);
	fade_sdl_surface(screen, SCROGUI_DEFAULT_FADE);
	if (strlen(leveled_config.sli_last_file_opened) != 0)
	{
		sprintf(filename_string, "%s", leveled_config.sli_last_file_opened);
	}
	file_open_dialog->addInput("input1", "Enter file name: ", filename_string, 40, 20, 20, 0);
	
	file_open_dialog->addButton("button1", "Open", 200, 100, NULL);
	file_open_dialog->addButton("button2", "Abort", 40, 100, NULL);
	
	file_open_dialog->redrawGUI(NULL);
	SDL_Flip(screen);
	while(!quit)
	{
		if (SDL_PollEvent(&event))
		{
			file_open_dialog->guiEventHandler(&event);
			file_open_dialog->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());
		if ((file_open_dialog->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED) ||
		    (file_open_dialog->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED))
		{
		    quit = 1;
		}
	}
	if ((file_open_dialog->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED))
	{
		leveled_ret = leveled_editor(filename_string);
		gui_pane.w = 300;
		gui_pane.h = 150;
		scrogui * file_not_exists = new scrogui(&gui_pane, screen, "Error");
		quit = 0;
		file_not_exists->addLabel("label1", "File does not exist!", 40, 20);
		file_not_exists->addButton("button1", "Abort", 40, 100, NULL);
		switch(leveled_ret)
		{
			case 2:
				file_not_exists->redrawGUI(NULL);
				SDL_Flip(screen);
				while(!quit)
				{
					if (SDL_PollEvent(&event))
					{
						file_not_exists->guiEventHandler(&event);
						file_not_exists->redrawGUI(NULL);
						SDL_Flip(screen);
					}
					SDL_Delay(TimeLeft());
					if (file_not_exists->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED) quit = 1;
				}
			break;
			
			default:
				sprintf(leveled_config.sli_last_file_opened, "%s", filename_string);
				leveled_config_save_settings(leveled_config);
			break;
		}
		delete(file_not_exists);
	}
	delete(file_open_dialog);
}


unsigned int sei_open_dialog(unsigned int parameter)
{
	unsigned int quit = 0;
	unsigned int leveled_ret = 0;
	SDL_Rect gui_pane;
	char * filename_string = new char [255];
	strcpy(filename_string, "enemies.sei");
	gui_pane.x = 200;
	gui_pane.y = 200;
	gui_pane.w = 300;
	gui_pane.h = 150;
	scrogui * file_open_dialog = new scrogui(&gui_pane, screen, "Enter file to open");
	file_open_dialog->setBlank(0);
	fade_sdl_surface(screen, SCROGUI_DEFAULT_FADE);
	file_open_dialog->addInput("input1", "Enter file name: ", filename_string, 40, 20, 20, 0);
	file_open_dialog->addButton("button1", "Open", 200, 100, NULL);
	file_open_dialog->addButton("button2", "Abort", 40, 100, NULL);
	file_open_dialog->redrawGUI(NULL);
	SDL_Flip(screen);
	while(!quit)
	{
		if (SDL_PollEvent(&event))
		{
			file_open_dialog->guiEventHandler(&event);
			file_open_dialog->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());
		if ((file_open_dialog->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED) ||
		    (file_open_dialog->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED))
		{
		    quit = 1;
		}
	}

	if (file_open_dialog->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED)
	{
		leveled_ret = sei_editor(filename_string);
		gui_pane.w = 300;
		gui_pane.h = 150;
		scrogui * file_not_exists = new scrogui(&gui_pane, screen, "Error");
		quit = 0;
		file_not_exists->addLabel("label1", "File does not exist!", 40, 20);
		file_not_exists->addButton("button1", "Abort", 40, 100, NULL);
		switch(leveled_ret)
		{
			case 2:
				printf("ret was 2\n");
				file_not_exists->redrawGUI(NULL);
				SDL_Flip(screen);
				while(!quit)
				{
					if (SDL_PollEvent(&event))
					{
						file_not_exists->guiEventHandler(&event);
						file_not_exists->redrawGUI(NULL);
						SDL_Flip(screen);
					}
					SDL_Delay(TimeLeft());
					if (file_not_exists->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED) quit = 1;
				}
				
			break;
			
			default:
			break;
		}
		delete(file_not_exists);
	}
	delete(file_open_dialog);
}

unsigned int sei_editor(char * filename)
{
	unsigned int quit = 0;
	char enemy_name[50];
	char sprites_file[255];
	char standard_sprites_from[5];
	char standard_sprites_to[5];
	char fireing_sprites_from[5];
	char fireing_sprites_to[5];
	char exploding_sprites_from[5];
	char exploding_sprites_to[5];
	char life_points[5];
	char maximum_speed[5];
	char sprite_size_x[5];
	char sprite_size_y[5];
	enemy_collection_object * root;
	enemy_collection_object * temp_ptr;
	unsigned int amount_enemies = 0;
	unsigned int button_counter = 0;
	char temp_string[50];
	char temp_string2[50];
	unsigned int slot_button_check = 0;
	unsigned int slot_button_select = 1;
	unsigned int element_counter = 0;
	sprintf(enemy_name, "\0");
	sprintf(sprites_file, "\0");
	sprintf(standard_sprites_from, "0");
	sprintf(standard_sprites_to, "0");
	sprintf(fireing_sprites_from, "0");
	sprintf(fireing_sprites_to, "0");
	sprintf(exploding_sprites_from, "0");
	sprintf(exploding_sprites_to, "0");
	sprintf(life_points, "100");
	sprintf(maximum_speed, "10");
	sprintf(sprite_size_x, "0");
	sprintf(sprite_size_y, "0");
	SDL_Rect gui_pane;
	gui_pane.x = 10;
	gui_pane.y = 10;
	gui_pane.w = 780;
	gui_pane.h = 580;
	scrogui * sei_ed_gui = new scrogui(&gui_pane, screen, "scro levelEd - enemy collection editor");
	sei_ed_gui->addButton("button3", "Write to file", 610, 535, NULL);
	sei_ed_gui->addButton("button1", "Abort", 725, 535, NULL);
	sei_ed_gui->addLabel("label1", "Enemies:", 20, 20);
//		wizard_menue->addInput("input1", "Enter file name: ", filename_string, 30, 40, 20, 0);
	
	// load enemy objects to see how many we have...
	fprintf(stderr, "will now atempt to load file...\n");
	root = sei_load_collection(filename);
	fprintf(stderr, "success, counting objects...");
	amount_enemies = sei_count_enemies(root);
	fprintf(stderr, "%d\n", amount_enemies);

	sprintf(enemy_name, "%s", root->data.name);
	sprintf(sprites_file, "%s", root->data.animation_data);
	sprintf(standard_sprites_from, "%s", root->data.std_sprite_from);
	sprintf(standard_sprites_to, "%s", root->data.std_sprite_to);
	sprintf(fireing_sprites_from, "%s", root->data.fir_sprite_from);
	sprintf(fireing_sprites_to, "%s", root->data.fir_sprite_to);
	sprintf(exploding_sprites_from, "%s", root->data.exp_sprite_from);
	sprintf(exploding_sprites_to, "%s", root->data.exp_sprite_to);
	sprintf(life_points, "%s", root->data.life_points);
	sprintf(maximum_speed, "%s", root->data.maximum_speed);
	sprintf(sprite_size_x, "%s", root->data.sprite_size_w);
	sprintf(sprite_size_y, "%s", root->data.sprite_size_h);

	while (button_counter < amount_enemies)
	{
		button_counter++;
		sprintf(temp_string, "button_slot%d", button_counter);
		sprintf(temp_string2, "Slot %d  ", button_counter);
		sei_ed_gui->addButton(temp_string, temp_string2, 25, 30+button_counter*20, NULL);
	}

	temp_ptr = root;

	sei_ed_gui->updateString("button_slot1", "Slot 1 *");
	sei_ed_gui->addInput("slot_input01", "Name: ", enemy_name, 150, 50, 50, 0);
	sei_ed_gui->addInput("slot_input02", "Sprites File: ", sprites_file, 150, 70, 50, 0);
	sei_ed_gui->addLabel("slot_label04", "Single Sprite size", 150, 90);
	sei_ed_gui->addInput("slot_input11", "w ", sprite_size_x, 280, 90, 3, 1);
	sei_ed_gui->addInput("slot_input12", "h ", sprite_size_y, 350, 90, 3, 1);
	sei_ed_gui->addLabel("slot_label01", "Standard Sprites", 150, 110);
	sei_ed_gui->addInput("slot_input03", "from ", standard_sprites_from, 280, 110, 3, 1);
	sei_ed_gui->addInput("slot_input04", "to ", standard_sprites_to, 350, 110, 3, 1);
	sei_ed_gui->addLabel("slot_label02", "Fireing Sprites", 150, 130);
	sei_ed_gui->addInput("slot_input05", "from ", fireing_sprites_from, 280, 130, 3, 1);
	sei_ed_gui->addInput("slot_input06", "to ", fireing_sprites_to, 350, 130, 3, 1);
	sei_ed_gui->addLabel("slot_label03", "Exploding Sprites", 150, 150);
	sei_ed_gui->addInput("slot_input07", "from ", exploding_sprites_from, 280, 150, 3, 1);
	sei_ed_gui->addInput("slot_input08", "to ", exploding_sprites_to, 350, 150, 3, 1);
	sei_ed_gui->addInput("slot_input09", "Life points: ", life_points, 150, 170, 4, 1);
	sei_ed_gui->addInput("slot_input10", "Maximum Speed: ", maximum_speed, 150, 190, 3, 1);

	sei_ed_gui->redrawGUI(NULL);
	SDL_Flip(screen);
	while(quit == 0)
	{
		if (SDL_PollEvent(&event))
		{
			sei_ed_gui->guiEventHandler(&event);
			
			if (sei_ed_gui->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED)
			{
				quit = 1;
			} else if (sei_ed_gui->get_flags_of_object("button3") & SCROGUI_BUTTON_PRESSED) /* Write to file, was pressed */
			{
				if (sei_save_collection(filename, root))
				{
					/* success */
					sei_ed_gui->message_box("Save", "Writing to file was successful!", 0);
				} else
				{
					/* error */
					sei_ed_gui->message_box("Save", "Writing to file FAILED!", 0);
				}
				sei_ed_gui->set_flags_of_object("button3", sei_ed_gui->get_flags_of_object("button3") ^ SCROGUI_BUTTON_PRESSED);
			} else 
			{
				while (slot_button_check < amount_enemies)
				{
					slot_button_check++;
					sprintf(temp_string, "button_slot%d", slot_button_check);
					if (sei_ed_gui->get_flags_of_object(temp_string) & SCROGUI_BUTTON_PRESSED)
					{
						sei_ed_gui->set_flags_of_object(temp_string, sei_ed_gui->get_flags_of_object(temp_string) ^ SCROGUI_BUTTON_PRESSED);
						sprintf(temp_string2, "Slot %d *", slot_button_check);
						sei_ed_gui->updateString(temp_string, temp_string2);
						sprintf(temp_string, "button_slot%d", slot_button_select);
						sprintf(temp_string2, "Slot %d  ", slot_button_select);
						sei_ed_gui->updateString(temp_string, temp_string2);						
						slot_button_select = slot_button_check;
						slot_button_check = amount_enemies;
						
						// apply values
						sprintf(temp_ptr->data.name, "%s", sei_ed_gui->returnValue("slot_input01"));
						sprintf(temp_ptr->data.animation_data, "%s", sei_ed_gui->returnValue("slot_input02"));
						sprintf(temp_ptr->data.std_sprite_from, "%s", sei_ed_gui->returnValue("slot_input03"));
						sprintf(temp_ptr->data.std_sprite_to, "%s", sei_ed_gui->returnValue("slot_input04"));
						sprintf(temp_ptr->data.fir_sprite_from, "%s", sei_ed_gui->returnValue("slot_input05"));
						sprintf(temp_ptr->data.fir_sprite_to, "%s", sei_ed_gui->returnValue("slot_input06"));
						sprintf(temp_ptr->data.exp_sprite_from, "%s", sei_ed_gui->returnValue("slot_input07"));
						sprintf(temp_ptr->data.exp_sprite_to, "%s", sei_ed_gui->returnValue("slot_input08"));
						sprintf(temp_ptr->data.life_points, "%s", sei_ed_gui->returnValue("slot_input09"));
						sprintf(temp_ptr->data.maximum_speed, "%s", sei_ed_gui->returnValue("slot_input10"));
						sprintf(temp_ptr->data.sprite_size_w, "%s", sei_ed_gui->returnValue("slot_input11"));
						sprintf(temp_ptr->data.sprite_size_h, "%s", sei_ed_gui->returnValue("slot_input12"));
						
						// go to correct element
						temp_ptr = root;
						element_counter = 1;
						while(element_counter < slot_button_select)
						{
							temp_ptr = temp_ptr->next;
							element_counter++;
						}
						
						// update gui elements...
						//sprintf(temp_string, "%s", temp_ptr->data.name);
						sei_ed_gui->updateValue("slot_input01", temp_ptr->data.name);
						sei_ed_gui->updateValue("slot_input02", temp_ptr->data.animation_data);
						sei_ed_gui->updateValue("slot_input03", temp_ptr->data.std_sprite_from);
						sei_ed_gui->updateValue("slot_input04", temp_ptr->data.std_sprite_to);
						sei_ed_gui->updateValue("slot_input05", temp_ptr->data.fir_sprite_from);
						sei_ed_gui->updateValue("slot_input06", temp_ptr->data.fir_sprite_to);
						sei_ed_gui->updateValue("slot_input07", temp_ptr->data.exp_sprite_from);
						sei_ed_gui->updateValue("slot_input08", temp_ptr->data.exp_sprite_to);
						sei_ed_gui->updateValue("slot_input09", temp_ptr->data.life_points);
						sei_ed_gui->updateValue("slot_input10", temp_ptr->data.maximum_speed);
						sei_ed_gui->updateValue("slot_input11", temp_ptr->data.sprite_size_w);
						sei_ed_gui->updateValue("slot_input12", temp_ptr->data.sprite_size_h);
					}
				}
				slot_button_check = 0;

			}
			sei_ed_gui->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());		
	}
//	delete(root);
}


unsigned int leveled_editor(char * filename)
{
	unsigned int quit = 2;
	unsigned int quit2 = 0;
	Scroll_LevelType * scro_level = sli_return_level(filename);
    SDL_Surface * gui_image;
	if (scro_level != NULL)
	{
		SDL_Rect gui_pane;
		SDL_Rect tile_menue_pane;
		SDL_Rect scroll_offset;
		SDL_Rect sei_dialog_pane;
		SDL_Rect sei_menue_pane;
		struct sli_header level_header = sli_load_header(filename);
		scrogui_blank_screen(NULL);
		unsigned int tile_counter_x = 0;
		unsigned int tile_counter_y = 0;
		char temp_string[30];
		unsigned int tile_counter = 0;
		unsigned int tile_pressed_check = 0;
		unsigned int almighty_tile = 0;
		unsigned int level_was_altered = 0;
		unsigned int want_quit = 0;
		unsigned int tile_menue_quit = 0;
		unsigned int enemy_menue_quit = 0;
		unsigned int open_toolbox = 0;
		unsigned int chosen_tile_x_start = 0;
		unsigned int chosen_tile_y_start = 0;
		unsigned int chosen_tile_x = 0;
		unsigned int chosen_tile_y = 0;
		unsigned int temp_uint = 0;
		unsigned int temp_uint2 = 0;
		unsigned int editor_mode = 0;
		unsigned int sei_dialog_create_quit = 0;
		unsigned int viewport_tiles_x = 0;
		unsigned int viewport_tiles_y = 0;
		SDL_Surface * toolbar_temp;
		
		viewport_tiles_x = level_header.dim_x;
		viewport_tiles_y = level_header.dim_y;
		
		if (viewport_tiles_x > 25) viewport_tiles_x = 25;
		if (viewport_tiles_y > 18) viewport_tiles_y = 18;
		
		Scroll * scroll_engine = new Scroll(viewport_tiles_x, viewport_tiles_y, scro_level);
		
		quit = 0;
		sei_dialog_pane.x = 200;
		sei_dialog_pane.y = 200;
		sei_dialog_pane.w = 400;
		sei_dialog_pane.h = 150;
		gui_pane.x = 0;
		gui_pane.y = 0;
		gui_pane.w = XMAX - 1;
		gui_pane.h = 24;
		tile_menue_pane.x = 50;
		tile_menue_pane.y = 50;
		tile_menue_pane.w = 420;
		tile_menue_pane.h = 335;
		sei_menue_pane.x = 50;
		sei_menue_pane.y = 50;
		sei_menue_pane.w = 420;
		sei_menue_pane.h = 335;
		scroll_offset.x = 0;
		scroll_offset.y = 24;
		scroll_offset.w = 0;
		scroll_offset.h = 0;
		scroll_engine->Blit(screen, scroll_offset);
		scrogui * ed_menue = new scrogui(&gui_pane, screen, "");
		scrogui * tile_menue = new scrogui(&tile_menue_pane, screen, "Available Tiles");
		scrogui * sei_dialog_create = new scrogui(&sei_dialog_pane, screen, "Select enemy collection");
		scrogui * enemy_menue = new scrogui(&sei_menue_pane, screen, "Available Enemies");
		//ed_menue->setAlpha(200);
		
		sei_dialog_create->setBlank(0);
		ed_menue->setBlank(0);
		tile_menue->setBlank(0);
		enemy_menue->setBlank(0);
		
		sei_dialog_create->addLabel("label1", "You did not yet specify an enemy collection.", 20, 10);
		sei_dialog_create->addInput("input1", "Enter file name: ", level_header.enemy_map, 40, 40, 20, 0);
		sei_dialog_create->addButton("button1", "Select", 300, 100, NULL);
		sei_dialog_create->addButton("button2", "Cancel", 40, 100, NULL);

		enemy_menue->addButton("button1", "Close", 370, 300, NULL);
		
//		ed_menue->addLabel("label1", "Chosen Tile", 12, 20);
		tile_menue->addLabel("label1", "Current Tile:", 15, 20);
		tile_menue->addImage("image1", scroll_engine->level->tiles_array[0], 135, 20);
//		ed_menue->addLabel("label2", "Tile palette", 12, 80);
		
//		ed_menue->addButton("button1", "Hide Toolbox", 20, 410, NULL);
//		ed_menue->addButton("button4", "Save", 20, 430, NULL);
		tile_menue->addButton("button1", "Hide", 370, 300, NULL);
		toolbar_temp = IMG_Load("data/graphics/tb_save.png");
		ed_menue->addImageButton("button6", toolbar_temp, XMAX - 75, 4, NULL);
		SDL_FreeSurface(toolbar_temp);
		toolbar_temp = IMG_Load("data/graphics/tb_info.png");
		ed_menue->addImageButton("button2", toolbar_temp, XMAX - 50, 4, NULL);
		SDL_FreeSurface(toolbar_temp);
		toolbar_temp = IMG_Load("data/graphics/tb_close.png");
		ed_menue->addImageButton("button4", toolbar_temp, XMAX - 25, 4, NULL);
		SDL_FreeSurface(toolbar_temp);
		toolbar_temp = IMG_Load("data/graphics/tb_tiles.png");
		ed_menue->addImageButton("button5", toolbar_temp, 10, 4, NULL);
		SDL_FreeSurface(toolbar_temp);
		toolbar_temp = IMG_Load("data/graphics/tb_enemy.png");
		ed_menue->addImageButton("button7", toolbar_temp, 35, 4, NULL);
		SDL_FreeSurface(toolbar_temp);
		toolbar_temp = IMG_Load("data/graphics/tb_objects.png");
		ed_menue->addImageButton("button8", toolbar_temp, 60, 4, NULL);
		SDL_FreeSurface(toolbar_temp);
//		ed_menue->addButton("button1", "[Tile  mode]", 10, 4, NULL);

		while ((tile_counter_y < 6) && (tile_counter_y*LEVELED_TILES_IN_ROW + tile_counter_x < scroll_engine->level->tiles_array_size))
		{
		    tile_counter++;
		    sprintf(temp_string, "tile%d", tile_counter);
		    tile_menue->addImageButton(temp_string, scroll_engine->level->tiles_array[tile_counter_y*LEVELED_TILES_IN_ROW + tile_counter_x], 15 + tile_counter_x*40, 60+tile_counter_y*40, NULL);
		    tile_counter_x++;
		    
		    if (tile_counter_x >= LEVELED_TILES_IN_ROW)
		    {
				tile_counter_x = 0;
				tile_counter_y++;
		    }
		}
		ed_menue->redrawGUI(NULL);
		SDL_Flip(screen);
		while (quit == 0)
		{
			if (SDL_PollEvent(&event))
			{
				if (event.type == SDL_KEYDOWN)
				{
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							scroll_engine->Move(0,-5);
							scroll_engine->Blit(screen, scroll_offset);
							//SDL_Flip(screen);
						break;
						
						case SDLK_DOWN:
							scroll_engine->Move(0,5);
							scroll_engine->Blit(screen, scroll_offset);
//							SDL_Flip(screen);
						break;
						
						case SDLK_RIGHT:
							scroll_engine->Move(5,0);
							scroll_engine->Blit(screen, scroll_offset);
						break;
						
						case SDLK_LEFT:
							scroll_engine->Move(-5,0);
							scroll_engine->Blit(screen, scroll_offset);
						break;
						
						case SDLK_PAGEUP:
							scroll_engine->Move(0,-128);
							scroll_engine->Blit(screen, scroll_offset);
	//						SDL_Flip(screen);
						break;
						
						case SDLK_PAGEDOWN:
							scroll_engine->Move(0,128);
							scroll_engine->Blit(screen, scroll_offset);
		//					SDL_Flip(screen);
						break;
						
						case SDLK_RETURN:
							open_toolbox = 1;
						break;
						
						case SDLK_ESCAPE:
						    want_quit = 1;
						break;
					}
				}   else if (event.type == SDL_MOUSEBUTTONDOWN)
				{
				    
				    if ((event.motion.y > scroll_offset.y) && (event.motion.x > scroll_offset.x))
				    {
				    	if (editor_mode == 0)
				    	{
							switch(event.button.button)
							{
								case SDL_BUTTON_LEFT:
									chosen_tile_x_start = (event.motion.x - scroll_offset.x + scroll_engine->screen.x) / 32 + scroll_engine->buffer_x_t; // not very accurate
									chosen_tile_y_start = (event.motion.y - scroll_offset.y + scroll_engine->screen.y) / 32 + scroll_engine->buffer_y_t;
								break;
							}
						}
					}
				}    else if (event.type == SDL_MOUSEBUTTONUP)
				{
				    if ((event.motion.y > scroll_offset.y) && (event.motion.x > scroll_offset.x))
				    {
				    	if (editor_mode == 0)
						{
						//    unsigned int chosen_tile_x = 0;
						//    unsigned int chosen_tile_y = 0;
							// calc tile pscroll_engine->screen.xosition :/
							// x of mouse is event.motion.x
							// y of mouse is event.motion.y
							//printf("mouse button pressed:\n");
							//printf("mouse coordinates are: %d %d\n", event.motion.x, event.motion.y);
							//printf("tile size is: %dx%d\n", scroll_engine->tile_width, scroll_engine->tile_height);
							//printf("level height in tiles is: %d\n", scroll_engine->level->level_height_t);


							switch(event.button.button)
							{
								case SDL_BUTTON_LEFT:
								 	printf("tile offset in pixel: %d %d, ", scroll_engine->screen.x, scroll_engine->screen.y);
								 	printf("level offset in tiles: %d %d\n", scroll_engine->buffer_x_t, scroll_engine->buffer_y_t);
								 	//printf("left button pressed.\n");
								 	//scroll_engine->level->tiles_array[tile_pressed_check-1]
								 	chosen_tile_x = (event.motion.x - scroll_offset.x + scroll_engine->screen.x) / 32 + scroll_engine->buffer_x_t; // not very accurate
								 	chosen_tile_y = (event.motion.y - scroll_offset.y + scroll_engine->screen.y) / 32 + scroll_engine->buffer_y_t;
								 	printf("selection from %d %d to %d %d\n", chosen_tile_x_start, chosen_tile_y_start, chosen_tile_x, chosen_tile_y);
								 	printf("almighty tile is %d\n", almighty_tile);
							
							
									//SDL_BlitSurface(scroll_engine->level->tiles_array[almighty_tile], NULL, scroll_engine->level->tile_surface[chosen_tile_y+1][chosen_tile_x], NULL);
							
									// dafür sorgen, dass _start immer die links obere ecke ist
									if (chosen_tile_x < chosen_tile_x_start)
									{
										temp_uint = chosen_tile_x;
										chosen_tile_x = chosen_tile_x_start;
										chosen_tile_x_start = temp_uint;
									}
									if (chosen_tile_y < chosen_tile_y_start)
									{
										temp_uint = chosen_tile_y;
										chosen_tile_y = chosen_tile_y_start;
										chosen_tile_y_start = temp_uint;
									}
							
									// hilfsvariablen setzen
									temp_uint = chosen_tile_x_start;
									temp_uint2 = chosen_tile_y_start;
								
								// lasset dass umbiegen beginnen
									while (temp_uint2 <= chosen_tile_y)
									{
										while(temp_uint <= chosen_tile_x)
										{
											// pointer umbiegen
											scroll_engine->level->tile_surface[temp_uint2][temp_uint] = scroll_engine->level->tiles_array[almighty_tile];
											// bytearray umschreiben
											scroll_engine->level->level_bytearray[(temp_uint2+1)*scroll_engine->level->level_width_t  + temp_uint] = almighty_tile;
											temp_uint++;
										}
										temp_uint = chosen_tile_x_start;
										temp_uint2++;
									}
									/*// pointer umbiegen
										scroll_engine->level->tile_surface[chosen_tile_y+1][chosen_tile_x] = scroll_engine->level->tiles_array[almighty_tile];
									// bytearray umschreiben
										scroll_engine->level->level_bytearray[(chosen_tile_y+1)*scroll_engine->level->level_width_t  + chosen_tile_x] = almighty_tile;
									*/
									// scroll engine refreshen
									scroll_engine->RefreshWholeBuffer();
									level_was_altered = 1;
							
									scroll_engine->Blit(screen, scroll_offset);
									rectangleRGBA(screen, scroll_offset.x + (chosen_tile_x_start-scroll_engine->buffer_x_t-1)*32-1+(scroll_engine->tile_width-scroll_engine->screen.x), scroll_offset.y+(chosen_tile_y_start-scroll_engine->buffer_y_t-1)*32-1+(scroll_engine->tile_height-scroll_engine->screen.y), 
									scroll_offset.x+(chosen_tile_x-scroll_engine->buffer_x_t)*32+1+(scroll_engine->tile_width-scroll_engine->screen.x), (chosen_tile_y-scroll_engine->buffer_y_t)*32+1+(scroll_engine->tile_height-scroll_engine->screen.y) + scroll_offset.y, 255, 255, 255, 255);
								  //  SDL_Flip(screen);
								break;
					
								case SDL_BUTTON_RIGHT:
									open_toolbox = 1;
								break;
					
								case SDL_BUTTON_WHEELUP:
									scroll_engine->Move(0,-5);
									scroll_engine->Blit(screen, scroll_offset);
									//SDL_Flip(screen);
								break;
					
								case SDL_BUTTON_WHEELDOWN:
									scroll_engine->Move(0,5);
									scroll_engine->Blit(screen, scroll_offset);
								   // SDL_Flip(screen);
								break;
							}
						}
					}
				}
				ed_menue->guiEventHandler(&event);
				
/*				if (ed_menue->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED)
				{
//					leveled_show_info(filename);
//					fade_sdl_surface(screen, 120);
					if (editor_mode == 1)
					{
						editor_mode = 0;
						ed_menue->updateString("button1", "[Tile  mode]");
					} else
					{
						editor_mode = 1;
						ed_menue->updateString("button1", "[Enemy mode]");
						if (strlen(level_header.enemy_map) == 0)
						{
							// did not select a collection yet
							fade_sdl_surface(screen, 120);
							sei_dialog_create->redrawGUI(NULL);
							SDL_Flip(screen);
							while(!sei_dialog_create_quit)
							{
								if (SDL_PollEvent(&event))
								{
									sei_dialog_create->guiEventHandler(&event);
									sei_dialog_create->redrawGUI(NULL);
									SDL_Flip(screen);
								}
								SDL_Delay(TimeLeft());
								if ((sei_dialog_create->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED) ||
									(sei_dialog_create->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED))
								{
									if ((strlen(sei_dialog_create->returnValue("input1")) == 0) &&
										(sei_dialog_create->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED))
									{
										sei_dialog_create->set_flags_of_object("button1", ed_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
										sei_dialog_create->message_box("Not valid", "Please enter a valid filename!", 0);
										ed_menue->redrawGUI(NULL);
									    scroll_engine->Blit(screen, scroll_offset);
										fade_sdl_surface(screen, 120);
										sei_dialog_create->redrawGUI(NULL);
										SDL_Flip(screen);
									} else if (sei_dialog_create->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED)
									{
										sei_dialog_create->set_flags_of_object("button2", ed_menue->get_flags_of_object("button2") ^ SCROGUI_BUTTON_PRESSED);
										ed_menue->updateString("button1", "[Tile  mode]");
										sei_dialog_create_quit = 1;
										editor_mode = 0;
										sprintf(level_header.enemy_map, "\0");
									} else
									{
										sei_dialog_create_quit = 1;
										level_was_altered = 1;
									}
					
								}
							}
						    scroll_engine->Blit(screen, scroll_offset);
						}
					}
					
					ed_menue->redrawGUI(NULL);
//					scroll_engine->Blit(screen, scroll_offset);
					ed_menue->set_flags_of_object("button1", ed_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
				}*/
				
				if (ed_menue->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED)
				{
					leveled_show_info(level_header);
					fade_sdl_surface(screen, 120);
					ed_menue->redrawGUI(NULL);
					scroll_engine->Blit(screen, scroll_offset);
					ed_menue->set_flags_of_object("button2", ed_menue->get_flags_of_object("button2") ^ SCROGUI_BUTTON_PRESSED);
				}
				if (ed_menue->get_flags_of_object("button7") & SCROGUI_BUTTON_PRESSED)
				{
					editor_mode = 1;
					fade_sdl_surface(screen, 120);
					enemy_menue->redrawGUI(NULL);
					SDL_Flip(screen);
					while(!enemy_menue_quit)
					{
						if (SDL_PollEvent(&event))
						{
							enemy_menue->guiEventHandler(&event);
							enemy_menue->redrawGUI(NULL);
							SDL_Flip(screen);
						}
						SDL_Delay(TimeLeft());
						if (enemy_menue->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED) enemy_menue_quit = 1;
					}
					enemy_menue->set_flags_of_object("button1", enemy_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
					enemy_menue_quit = 0;
					scroll_engine->Blit(screen, scroll_offset);
					ed_menue->set_flags_of_object("button7", ed_menue->get_flags_of_object("button7") ^ SCROGUI_BUTTON_PRESSED);
					
				}
				if (ed_menue->get_flags_of_object("button5") & SCROGUI_BUTTON_PRESSED)
				{
					editor_mode = 0;
					fade_sdl_surface(screen, 120);
					tile_menue->redrawGUI(NULL);
					SDL_Flip(screen);
				
					while(!tile_menue_quit)
					{
						if (SDL_PollEvent(&event))
						{
							tile_menue->guiEventHandler(&event);
							tile_pressed_check = 0;
							while (tile_pressed_check <= tile_counter)
							{
								tile_pressed_check++;
								sprintf(temp_string, "tile%d", tile_pressed_check);
								
								if (tile_menue->get_flags_of_object(temp_string) & SCROGUI_BUTTON_PRESSED)
								{
									printf("%s was pressed.\n", temp_string);
									tile_menue->set_flags_of_object(temp_string, tile_menue->get_flags_of_object(temp_string) ^ SCROGUI_BUTTON_PRESSED);
									tile_menue->updateImage("image1", scroll_engine->level->tiles_array[tile_pressed_check-1]);
									almighty_tile = tile_pressed_check - 1;
								}
								
							}
							tile_menue->redrawGUI(NULL);
							SDL_Flip(screen);
						}
						SDL_Delay(TimeLeft());
						if (tile_menue->get_flags_of_object("button1")) tile_menue_quit = 1;
					}
					tile_menue->set_flags_of_object("button1", tile_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
					tile_menue_quit = 0;
				
					ed_menue->redrawGUI(NULL);
					scroll_engine->Blit(screen, scroll_offset);
					ed_menue->set_flags_of_object("button5", ed_menue->get_flags_of_object("button5") ^ SCROGUI_BUTTON_PRESSED);
				}

				if (ed_menue->get_flags_of_object("button6") & SCROGUI_BUTTON_PRESSED)
				{
				    sli_save_level(filename, level_header, scroll_engine->level);
				    ed_menue->message_box("Level saved.", "The level file was sucessfully saved.", 0);
				    ed_menue->set_flags_of_object("button6", ed_menue->get_flags_of_object("button6") ^ SCROGUI_BUTTON_PRESSED);
				    scroll_engine->Blit(screen, scroll_offset);
				    ed_menue->redrawGUI(NULL);
				    SDL_Flip(screen);
				    level_was_altered = 0; 
				}
				
				
				if (ed_menue->get_flags_of_object("button4") & SCROGUI_BUTTON_PRESSED) want_quit = 1;
				ed_menue->redrawGUI(NULL);
				SDL_Flip(screen);

/*				if (open_toolbox)
				{
					fade_sdl_surface(screen, 120);
					ed_menue->redrawGUI(NULL);
					SDL_Flip(screen);
					while(!quit2)
					{
						if (SDL_PollEvent(&event))
						{
							ed_menue->guiEventHandler(&event);
							tile_pressed_check = 0;
							while (tile_pressed_check <= tile_counter)
							{
							    tile_pressed_check++;
							    sprintf(temp_string, "tile%d", tile_pressed_check);
							    
							    if (ed_menue->get_flags_of_object(temp_string) & SCROGUI_BUTTON_PRESSED)
							    {
								printf("%s was pressed.\n", temp_string);
								ed_menue->set_flags_of_object(temp_string, ed_menue->get_flags_of_object(temp_string) ^ SCROGUI_BUTTON_PRESSED);
								ed_menue->updateImage("image1", scroll_engine->level->tiles_array[tile_pressed_check-1]);
								almighty_tile = tile_pressed_check - 1;
							    }
							    
							}
							if (event.type == SDL_MOUSEBUTTONUP)
							{
							    if (event.button.button == SDL_BUTTON_RIGHT) quit2 = 1;
							}
							//fade_sdl_surface(screen, 120);
							ed_menue->redrawGUI(NULL);
							SDL_Flip(screen);
						}
						SDL_Delay(TimeLeft());
						if (ed_menue->get_flags_of_object("button4") & SCROGUI_BUTTON_PRESSED)
						{
						    sli_save_level(filename, scroll_engine->level);
						    ed_menue->message_box("Level saved.", "The level file was sucessfully saved.", 0);
						    ed_menue->set_flags_of_object("button4", ed_menue->get_flags_of_object("button4") ^ SCROGUI_BUTTON_PRESSED);
						    //quit = 1;
						    //quit2 = 1;
						    scroll_engine->Blit(screen, scroll_offset);
						    fade_sdl_surface(screen, 120);
						    ed_menue->redrawGUI(NULL);
						    SDL_Flip(screen);
						    level_was_altered = 0;
						    
						}
						if (ed_menue->get_flags_of_object("button1") & SCROGUI_BUTTON_PRESSED)
						{
						    ed_menue->set_flags_of_object("button1", ed_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
						    quit2 = 1;
						}
						if (ed_menue->get_flags_of_object("button3") & SCROGUI_BUTTON_PRESSED)
						{
							want_quit = 1;
							quit2 = 1;
						}
						if (ed_menue->get_flags_of_object("button2") & SCROGUI_BUTTON_PRESSED)
						{
							leveled_show_info(filename);
							scroll_engine->Blit(screen, scroll_offset);
							fade_sdl_surface(screen, 120);
							ed_menue->redrawGUI(NULL);
							SDL_Flip(screen);
							ed_menue->set_flags_of_object("button2", ed_menue->get_flags_of_object("button2") ^ SCROGUI_BUTTON_PRESSED);
						}
					}
					quit2 = 0;
					scroll_engine->Blit(screen, scroll_offset);
					SDL_Flip(screen);
//					if (leveled_ed_quit(0)) quit = 1;
					open_toolbox = 0;
				}*/
				if (want_quit)
				{
					if (level_was_altered)
					{
					    if (ed_menue->message_box("Save level?", "Do you want to save your level NOW?", 1))
					    {
							sli_save_level(filename, level_header, scroll_engine->level);
							ed_menue->message_box("Level saved.", "The level file was sucessfully saved.", 0);
					    }
					}
					quit = 1;
				}


			}
			if (ed_menue->get_flags_of_object("tile1") & SCROGUI_BUTTON_PRESSED)
			{
			    printf("this was tile1\n");
			    ed_menue->set_flags_of_object("tile1", ed_menue->get_flags_of_object("tile1") ^ SCROGUI_BUTTON_PRESSED);
			}
			SDL_Delay(TimeLeft());
		}
		delete scroll_engine;
		sli_free_level(scro_level);
		delete(ed_menue);
	}
	return quit;
}


unsigned int leveled_show_info(struct sli_header info_header)
{
	SDL_Rect info_pane;
	unsigned int quit = 0;
	info_pane.x = 200;
	info_pane.y = 100;
	info_pane.w = 270;
	info_pane.h = 210;
	char format_ver[5];
	char dims[100];
	scrogui * info_gui = new scrogui(&info_pane, screen, "Level Information");
//	struct sli_header info_header = sli_load_header(filename);
//	info_gui->addLabel("label1", "Filename", 20, 20);
//	info_gui->addLabel("label2", filename, 150, 20);
	info_gui->addLabel("label3", "Format version", 20, 40);
	sprintf(format_ver, "%d", info_header.version);
	info_gui->addLabel("label4", format_ver, 150, 40);
	info_gui->addLabel("label5", "Author", 20, 60);
	info_gui->addLabel("label6", info_header.author, 150, 60);
	info_gui->addLabel("label7", "Title", 20, 80);
	info_gui->addLabel("label8", info_header.title, 150, 80);

	info_gui->addLabel("label9", "Tileset", 20, 100);
	info_gui->addLabel("label10", info_header.tileset, 150, 100);

	info_gui->addLabel("label11", "Dimensions", 20, 120);
	sprintf(dims, "%dx%d", info_header.dim_x, info_header.dim_y);
	info_gui->addLabel("label12", dims, 150, 120);
	info_gui->addLabel("label13", "Enemy map", 20, 140);
	if (strlen(info_header.enemy_map) == 0)
	{
		info_gui->addLabel("label14", "none selected", 150, 140);	
	} else
	{
		info_gui->addLabel("label14", info_header.enemy_map, 150, 140);
	}
	info_gui->addButton("button1", "Return", 220, 170, NULL);

	//info_gui->setAlpha(200);
	info_gui->setBlank(0);
	fade_sdl_surface(screen, 120);
	info_gui->redrawGUI(NULL);
	SDL_Flip(screen);
	while(!quit)
	{
		if (SDL_PollEvent(&event))
		{
			info_gui->guiEventHandler(&event);
			info_gui->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());
		if (info_gui->get_flags_of_object("button1")) quit = 1;
	}
	delete(info_gui);
/*
	scrogui_label(prim_font, "Level format version:", &text_dest, color);
	text_dest.x = 360;
	sprintf(temp, "%d", info_header.version);
	scrogui_label(prim_font, temp, &text_dest, color);

	text_dest.x = 50;
	text_dest.y = 270;
	scrogui_label(prim_font, "Dimension:", &text_dest, color);
	text_dest.x = 360;
	sprintf(temp, "%dx%d", info_header.dim_x, info_header.dim_y);
	scrogui_label(prim_font, temp, &text_dest, color);

*/
}

void scrogui_blank_screen(SDL_Rect *blankrect = NULL)
{
	SDL_Surface *blank = SDL_CreateRGBSurface(SDL_HWSURFACE, 800, 600, 16, 0, 0, 0, 0);
	SDL_BlitSurface(blank, blankrect, screen, blankrect);
	SDL_FreeSurface(blank);
}


unsigned int wizard_generate(unsigned int parameter)
{
	unsigned int quit = 0;
	char * filename_string = new char [255];
	char * length_string = new char [10];
	char * width_string = new char [10];
	unsigned int overwrite = 0;
	strcpy(filename_string, "level.sli");
	strcpy(length_string, "150");
	strcpy(width_string, "25");
	SDL_Rect gui_pane;
	gui_pane.x = 200;
	gui_pane.y = 200;
	gui_pane.w = 400;
	gui_pane.h = 200;
	scrogui * wizard_menue = new scrogui(&gui_pane, screen, "Generate a level");
//	wizard_menue->setBlank(0);
//	fade_sdl_surface(screen, SCROGUI_DEFAULT_FADE);
	wizard_menue->addInput("input1", "Enter file name: ", filename_string, 30, 40, 20, 0);
	wizard_menue->addInput("input2", "Enter length (in tiles): ", length_string, 30, 60, 5, 1);
	wizard_menue->addInput("input3", "Enter width (in tiles): ", width_string, 30, 80, 5, 1);
	wizard_menue->addButton("button1", "Create", 30, 100, NULL);
	wizard_menue->addButton("button2", "Back", 30, 130, NULL);
	wizard_menue->redrawGUI(NULL);
	SDL_Flip(screen);
	while (quit == FALSE)
	{
		if (SDL_PollEvent(&event))
		{
			wizard_menue->guiEventHandler(&event);
			wizard_menue->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());
		if (wizard_menue->get_flags_of_object("button2")) quit = 1;
		if (wizard_menue->get_flags_of_object("button1"))
		{
		    
		    if (fopen(filename_string, "r") != NULL)
		    {
				if (wizard_menue->message_box("Question", "File already exists, overwrite?", 1)) 
				{
					overwrite = 1;
			   	} else
			    {
					overwrite = 2;
			    }
			}
	    	
			if (overwrite != 2)
			{
			    if (sli_generate_level(filename_string, atoi(width_string), atoi(length_string)) == 0)
			    {
				    wizard_menue->message_box("Success!", "Level was generated.", 0);
				    quit = 1;
			    } else
			    {
				    wizard_menue->message_box("Failure!", "An error occured.", 0);
			    }
			} else
			{
			    wizard_menue->message_box("Failure!", "Aborted by user.", 0);
			}
		    overwrite = 0;
		    wizard_menue->set_flags_of_object("button1", wizard_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
		    //wizard_menue->setBlank(1);
		    wizard_menue->redrawGUI(NULL);
		}
	}
	delete(wizard_menue);
	return 1;
}



unsigned int wizard_create_sei(unsigned int parameter)
{
	unsigned int quit = 0;
	char * filename_string = new char [255];
	unsigned int overwrite = 0;
	strcpy(filename_string, "enemies.sei");
	SDL_Rect gui_pane;
	gui_pane.x = 200;
	gui_pane.y = 200;
	gui_pane.w = 400;
	gui_pane.h = 200;
	scrogui * wizard_menue = new scrogui(&gui_pane, screen, "Create a new enemy collection file");
	wizard_menue->addInput("input1", "Enter file name: ", filename_string, 30, 40, 20, 0);
	wizard_menue->addButton("button1", "Create", 30, 80, NULL);
	wizard_menue->addButton("button2", "Back", 30, 110, NULL);
	wizard_menue->redrawGUI(NULL);
	SDL_Flip(screen);
	while (quit == FALSE)
	{
		if (SDL_PollEvent(&event))
		{
			wizard_menue->guiEventHandler(&event);
			wizard_menue->redrawGUI(NULL);
			SDL_Flip(screen);
		}
		SDL_Delay(TimeLeft());
		if (wizard_menue->get_flags_of_object("button2")) quit = 1;
		if (wizard_menue->get_flags_of_object("button1"))
		{
		    
		    if (fopen(filename_string, "r") != NULL)
		    {
				if (wizard_menue->message_box("Question", "File already exists, overwrite?", 1)) 
				{
					overwrite = 1;
				} else
				{
					overwrite = 2;
				}
			}
	    	
			if (overwrite != 2)
			{
			    if (sei_create_collection(filename_string) == 1)
			    {
				    wizard_menue->message_box("Success!", "File was created.", 0);
				    quit = 1;
			    } else
			    {
				    wizard_menue->message_box("Failure!", "An error occured.", 0);
			    }
			} else
			{
			    wizard_menue->message_box("Failure!", "Aborted by user.", 0);
			}
		    
		    wizard_menue->set_flags_of_object("button1", wizard_menue->get_flags_of_object("button1") ^ SCROGUI_BUTTON_PRESSED);
		    //wizard_menue->setBlank(1);
		    wizard_menue->redrawGUI(NULL);
		}
	}
	delete(wizard_menue);
	return 1;
}




